package com.anlogic.sdk.utils;

public class BitsUtils {

	public static int getBit(int original, int offset) {
		return (original & (1 << offset)) >> offset;
	}
	
	public static int getBits(int original, int high, int low) {
		int mask_high = (1 << (high + 1)) - 1;
		return (mask_high & original) >> low;
	}
	
	public static int setBit(int ori, int offset, int value) {
		value = value & 1;
		int mask = ~(1 << offset);
		return (ori & mask) | (value << offset);
	}
	
	public static int setBits(int original, int high, int low, int value) {
		value = value & ((1 << (high - low + 1)) - 1); 
		int mask_high = (1 << (high + 1)) - 1;
		int mask_low = (1 << (low + 1)) - 1;
		int mask = ~(mask_high - mask_low);
		return (original & mask) | (value << low);
	}
}
